$(function () {
  $("#jqGrid").jqGrid({
    url: 'sys/generator/list',
    datatype: "json",
    colModel: [
      {label: '表名', name: 'tableName', width: 100, key: true},
      {label: '表备注', name: 'tableComment', width: 100},
      {label: '创建时间', name: 'createTime', width: 100}
    ],
    viewrecords: true,
    height: 385,
    rowNum: 10,
    rowList: [10, 30, 50, 100, 200],
    rownumbers: true,
    rownumWidth: 25,
    autowidth: true,
    multiselect: true,
    pager: "#jqGridPager",
    jsonReader: {
      root: "page.list",
      page: "page.currPage",
      total: "page.totalPage",
      records: "page.totalCount"
    },
    prmNames: {
      page: "page",
      rows: "limit",
      order: "order"
    },
    gridComplete: function () {
      //隐藏grid底部滚动条
      $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
    }
  });
});

var vm = new Vue({
  el: '#rrapp',
  data: {
    q: {
      tableName: null
    },
    domain: null,
    mode:null,
    errorShow:false
  },
  methods: {
    query: function () {
      $("#jqGrid").jqGrid('setGridParam', {
        postData: {'tableName': vm.q.tableName},
        page: 1
      }).trigger("reloadGrid");
    },
    generator: function () {
      var tableNames = getSelectedRows();
            if (tableNames == null) {
              return;
            }
            var domain = vm.domain
            if(!domain){
              vm.errorShow=true
              return;
            }
            vm.errorShow=false
      if(vm.mode==='code'){
        $('#myModal').modal('hide')
        location.href = "sys/generator/code?tables=" + tableNames.join()+"&domain="+domain.trim();
      }else if(vm.mode==='native'){
        $.get("sys/generator/native?tables=" + tableNames.join()+"&domain="+domain.trim(),function(data,status){
          if(data&&data.code===0){
             $('#myModal').modal('hide')
             $('.ant-message').show()
             setTimeout(function(){
             $('.ant-message').hide()
             },1500)
            }
          })
      }
    },
    openModal: function (mode) {
      vm.mode=mode;
      vm.domain=null
      vm.errorShow=false
      var tableNames = getSelectedRows();
      if (tableNames == null) {
        return;
      }
      $('#myModal').modal('show')
    },
    closeModal: function () {
       $('#myModal').modal('hide')
    },

  }
});

